package com.qf.smartplatform.config;

import com.qf.smartplatform.mapper.MenuMapper;
import com.qf.smartplatform.pojo.Menu;
import com.qf.smartplatform.security.handlers.MyAuthenSuccessHandler;
import com.qf.smartplatform.security.handlers.MyLoginFailureHandlder;
import com.qf.smartplatform.security.service.MyUserDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * projectName:day12_27_Test
 * author:guoLei
 * time:2022/1/9 17:34
 * description:
 */
@EnableWebSecurity
@Configuration
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {
    private MyUserDetailService myUserDetailService;
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    private MyLoginFailureHandlder myLoginFailureHandlder;
    private MyAuthenSuccessHandler myAuthenSuccessHandler;
    private MenuMapper menuMapper;

    @Autowired
    public void setMenuMapper(MenuMapper menuMapper) {
        this.menuMapper = menuMapper;
    }

    @Autowired
    public void setMyAuthenSuccessHandler(MyAuthenSuccessHandler myAuthenSuccessHandler) {
        this.myAuthenSuccessHandler = myAuthenSuccessHandler;
    }

    @Autowired
    public void setMyLoginFailureHandlder(MyLoginFailureHandlder myLoginFailureHandlder) {
        this.myLoginFailureHandlder = myLoginFailureHandlder;
    }
@Autowired
    public void setbCryptPasswordEncoder(BCryptPasswordEncoder bCryptPasswordEncoder) {
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
    }

    @Autowired
    public void setMyUserDetailService(MyUserDetailService myUserDetailService) {
        this.myUserDetailService = myUserDetailService;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        List<Menu> menus = menuMapper.selectAllMenu();
        menus.parallelStream().filter(menu -> StringUtils.hasText(menu.getUrl())).forEach(menu -> {
            try {
                http.authorizeRequests().antMatchers(menu.getUrl()).hasAuthority(menu.getPerms());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        http.authorizeRequests()
                .and().formLogin().failureHandler(myLoginFailureHandlder)
                .successHandler(myAuthenSuccessHandler)
                .loginProcessingUrl("/login")
                .permitAll()
                .and().authorizeRequests().anyRequest()
                .authenticated();
    }
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/index.html", "/css/**", "/js/**", "/websocket/*",
                "/humiture/*");
        super.configure(web);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(myUserDetailService).passwordEncoder(bCryptPasswordEncoder);
    }
}
